package patrol.mobile.com.mobilepatrol.activity.toll.add_data;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.DateBean;
import patrol.mobile.com.mobilepatrol.bean.GangweiKaoheBean;
import patrol.mobile.com.mobilepatrol.bean.GetPostListBean;
import patrol.mobile.com.mobilepatrol.bean.UserBean;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.utils.Constants;
import patrol.mobile.com.mobilepatrol.utils.DatePickerUtil02;
import patrol.mobile.com.mobilepatrol.utils.SPUtils;
import patrol.mobile.com.mobilepatrol.utils.TimeUtils;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.view.CallBack;
import patrol.mobile.com.mobilepatrol.widget.MySpinner;

/**
 * 岗位信息考核
 * 测试代码
 */
public class AddDataPage1Activity extends BaseActivity {
    @BindView(R.id.editDqDw)
    TextView editDqDw;
    @BindView(R.id.editDqsj)
    TextView editDqsj;
    @BindView(R.id.editKhgw)
    MySpinner editKhgw;
    @BindView(R.id.editBkhr)
    MySpinner editBkhr;
    @BindView(R.id.editKhr)
    TextView editKhr;
    @BindView(R.id.editDqbc)
    MySpinner editDqbc;
    @BindView(R.id.btn_next)
    Button btnNext;
    @BindView(R.id.editclass)
    TextView editclass;
    private GangweiKaoheBean gangweiKaoheBean;
    private GetPostListBean getPostListBean;
    private UserBean userBean;
    private int danweiIndex;
    private int kaoherenIndex;
    private int beikaoherenIndex;
    private int dangqianBanciIndex;
    private List<String> banchiList;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_add_data_page_layout;
    }

    @Override
    public void setListener() {

    }

    @Override
    public void initTitle() {
        super.initTitle();
        banchiList = new ArrayList<>();
        banchiList.add("第1班次");
        banchiList.add("第2班次");
        banchiList.add("第3班次");
        banchiList.add("第4班次");
        gangweiKaoheBean = new GangweiKaoheBean();
        tv_center_title.setVisibility(View.VISIBLE);
        iv_back.setVisibility(View.VISIBLE);
        tv_center_title.setText("岗位考核");
        editDqDw.setText(UserManager.getUser((String) SPUtils.get(Constants.current_id, "")).bmname);
        editKhr.setText(UserManager.getUser((String) SPUtils.get(Constants.current_id, "")).ryname);
        editDqsj.setText(TimeUtils.timeString(System.currentTimeMillis()));
        editDqbc.setmDatas(banchiList, new MySpinner.CallBack() {
            @Override
            public void onSelect(int position) {
                dangqianBanciIndex = position;
            }
        });
        editDqsj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DatePickerUtil02.getInstance(view.getContext()).setCallBack(new CallBack<DateBean>() {
                    @Override
                    public void onBack(DateBean dateBean) {
                        editDqsj.setText(dateBean.getmDate());
                    }
                });
            }
        });
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                gangweiKaoheBean.setBeikaoheren(userBean.getYhList().get(beikaoherenIndex).getRyname());
                gangweiKaoheBean.setBeikaoherenId(userBean.getYhList().get(beikaoherenIndex).getRyid());
                gangweiKaoheBean.setDangqianbanci(editclass.getText().toString());
                gangweiKaoheBean.setDanwei(editDqDw.getText().toString());
                gangweiKaoheBean.setKaohegangwei(getPostListBean.getList().get(danweiIndex).getPost());
                gangweiKaoheBean.setKaoheren(UserManager.getUser(current_id).ryname);
                gangweiKaoheBean.setShijain(editDqsj.getText().toString());
                startActivity(new Intent(view.getContext(), AddDataPage2Activity.class)
                        .putExtra("data", gangweiKaoheBean));
            }
        });
        getHttpDataGw();
        getUsers();
        requestDateData(UserManager.getOrgNo());
    }

    private void requestDateData(String orgId) {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.GET_WORK_TIME);
        HashMap<String, String> map = new HashMap<>();
        map.put("orgId", orgId);
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {
                Log.e("responedata", e.getMessage());
            }

            @Override
            public void onResponse(String code, String response) {
                Log.e("responedata", response.substring(1, response.length() - 1));
                if (!TextUtils.isEmpty(response)) {
                    Log.e("responedata", response.substring(1, response.length() - 1));
                    editclass.setText(response.substring(1, response.length() - 1));
                }
            }
        });
    }

    private void getHttpDataGw() {
        HashMap<String, String> hashMap = new HashMap();
        HttpSender sender = new HttpSender(this, baseUrl, Urls.getPostList);
        sender.requestPost(hashMap, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                getPostListBean = new Gson().fromJson(response, GetPostListBean.class);
                List<String> list = new ArrayList<>();
                for (GetPostListBean.ListBean listBean : getPostListBean.getList()) {
                    list.add(listBean.getPost());
                }
                editKhgw.setmDatas(list, new MySpinner.CallBack() {
                    @Override
                    public void onSelect(int position) {
                        danweiIndex = position;
                    }
                });
            }
        });
    }

    public void getUsers() {
        OkHttpUtils.get().url(SPUtils.get(Constants.current_ser, "") + Urls.getUsers).build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        userBean = new Gson().fromJson(response, UserBean.class);
                        List<String> list = new ArrayList<>();
                        for (UserBean.YhListBean yhListBean : userBean.getYhList()) {
                            list.add(yhListBean.getRyname());
                        }
                        editBkhr.setmDatas(list, new MySpinner.CallBack() {
                            @Override
                            public void onSelect(int position) {
                                beikaoherenIndex = position;
                            }
                        });
                    }
                });
    }


}
